#!/bin/bash

#// *************************************************************************
#// * COPYRIGHT 2004, EMULEX CORPORATION                                    *
#// * 3333 Susan Street, Costa Mesa, CA 92626                               *
#// *                                                                       *
#// * All rights reserved.  This computer program and related documentation *
#// * is protected by copyright  and distributed under licenses restricting *
#// * its use,  copying,  distribution  and decompilation.    This computer *
#// * program  and its  documentation  are CONFIDENTIAL  and a TRADE SECRET *
#// * of EMULEX CORPORATION.   The receipt or  possession of  this  program *
#// * or its documentation does not  convey rights to reproduce or disclose *
#// * its  contents,  or to  manufacture, use, or sell anything that it may *
#// * describe, in whole or in part,  without the specific  written consent *
#// * of  EMULEX CORPORATION.   Any reproduction  of  this program  without *
#// * the express  written  consent  of EMULEX  CORPORATION  is a violation *
#// * of the  copyright laws  and may  subject you to  criminal prosecution.*
#// *************************************************************************


opt="$1"

SSCInstall=0
kernelInstall=0
LOGFILE="/usr/src/lpfcdfc/utils-install.log"

# Function:  log_message()
#
# Description:
#   Print the strings passed as parameters to both STDOUT as well as
#   to a log file.
#
# Parameters:
#   A series of strings.
#
# Returns:
#   0 on success.
log_message()
{
    local ECHO_OPTIONS
    LOGDIRECTORY=${LOGFILE%/*}
    if [ ! -d ${LOGDIRECTORY} ] ; then
	mkdir -p ${LOGDIRECTORY}
    fi

    if [ $# -eq 0 ] ; then
	echo "" | tee -a ${LOGFILE}
    elif [ -z "$1" ] ; then
	echo "" | tee -a ${LOGFILE}
    elif [ "$1" = "-init" ] ; then
	echo "$0 - $(date)" >> ${LOGFILE}
    else
	while [ -n "$1" ] ; do
	    if [ "${1:0:1}" = "-" ] ; then
		ECHO_OPTIONS="${1}"
	    else
		echo ${ECHO_OPTIONS} "$1" | tee -a ${LOGFILE}
	    fi
	    shift
	done
    fi

    return 0
}

# Initialize the log file.
log_message -init

# Check if HBAnywareSSC install option is passed in
if [ -n "$opt" ];then
    opt=`echo $opt | tr A-Z a-z`
    if [ "$opt" = "ssc" ];then
        SSCInstall=1
    fi
    if [ "$opt" = "upgradekernel" ];then
        kernelInstall=1
    fi
fi

#
# This scripts installs HBAnyware and HBAnywareSSC for Linux. Also
# included our lpfc helper modules and libraries. This script
# also provides kernel upgrade capability
#
                                                                                                                        
# Get environment data
OS=`uname -s`
Arch=`uname -m`
if [ "$Arch" = "i686" ];then
    Arch="i386"
fi

if [ "$OS" = "Linux" ];then
        Kernel_Ver=`uname -r 2>&1 | awk '{ print $1 }' | awk -F. '{ print $1$2 }'`
fi

# obtain gcc version
# first test for japanes/english type
test_gcc=`gcc -v 2>&1 | grep "gcc " | awk '{ print $1 }'`
if [ "$test_gcc" = "gcc" ];then
    # english or japanese version
    GCC_Ver=`gcc -v 2>&1 | grep "gcc " | awk '{ print $3 }' | awk -F. '{ print $1 }'`
else
    # german version
    GCC_Ver=`gcc -v 2>&1 | grep gcc | grep ersion | awk '{ print $2 }' | awk -F. '{ print $1 }'`
fi

# default to gcc 3 (only version currently supported)
if [ "$GCC_Ver" -ne 2 ];then
   if [ "$GCC_Ver" -ne 3 ];then
      if [ "$GCC_Ver" -ne 4 ];then
          GCC_Ver="3"
      fi
   fi
fi

OS_Type=`if [ -d /usr/src/redhat ];then echo "RH";else echo "SL";fi`
if [ "$OS_Type" = "RH" ];then
    if [ "$Kernel_Ver" = "26" ]; then
        OS_Type="RH4"
        OS_Ver="rhel-4.0"
    else
        if [ "$GCC_Ver" -eq 2 ];then
            OS_Ver="rhel-2.1"
        else
            OS_Ver="rhel-3.0"
        fi
    fi
else
    if [ "$Kernel_Ver" = "26" ]; then
        OS_Type="SL9"
    	OS_Ver="sles-9.0"
    else
    	OS_Ver="sles-8.0"
    fi
fi

# This is a Linux install only, if we're not on Linux then stop
if [ "$OS" != "Linux" ];then
    log_message "This installation is for Linux only! Exiting..."
    exit
fi
                                                                                                                        
# Only proceed with SSC install if HBAnyware is installed
if [ $SSCInstall -eq 1 ];then
    Cur_HBA=`rpm -qa | grep HBAnyware | grep -v SSC`
    if [ -z "$Cur_HBA" ];then
        log_message "ERROR: HBAnyware not installed. HBAnyware must be installed before SSC can be installed"
        exit
    fi
fi

cd enterprise_kitfiles

# Untar and uncompress tgz file
log_message "Untarring EmlxApps tgz file..."
tar xfz EmlxApps*.tgz
#log_message ""

# Kernel Upgrade
if [ $kernelInstall -eq 1 ];then
     log_message "Upgrading Kernel"
	./lpfcdfc-install -k
    if [ $? -eq 0 ];then
        log_message "Success upgrading kernel."
    else
        log_message "Errors encountered upgrading kernel."
    fi
else 
    if [ $SSCInstall -eq 1 ];then
#      HBAnywareSSC Installation

       # Uninstall any previousely installed HBAnywareSSC
       Cur_HBA=`rpm -qa | grep HBAnywareSSC`
       if [ -n "$Cur_HBA" ];then
           log_message "Uninstalling HBAnywareSSC -- $Cur_HBA -- ..."
           rpm -e "$Cur_HBA"
       fi

       # Verify HBAnyware installed version matches candidate SSC version
       rpm_dir="./${Arch}_${OS_Type}_${GCC_Ver}.XX"
       cd "$rpm_dir"
       hbanyware_version=`rpm -qa | grep HBAnyware | grep -v SSC`
       hbanyware_version=`echo $hbanyware_version | awk -F\- '{ print $2 }'`
       ssc_version=`ls HBAnywareSSC*.rpm | awk -F\- '{ print $2 }'`
       ##echo "ssc version is $ssc_version"
       ##echo "hbanyware_version version is $hbanyware_version"

       if [ "$hbanyware_version" = "$ssc_version" ]; then
           # Install HBAnywareSSC
           rpm -U --nodeps HBAnywareSSC-*.rpm
           cd ..
           log_message ""

           log_message "HBAnywareSSC installation complete."
           log_message ""
       else
           echo "Error: version mismatch detected between installed HBAnyware and candidate SSC versions."
           echo "HBAnyware and HBAnyware SSC versions must match; exiting."
          exit
       fi
    else
       # HBAnyware Installation

       # Make sure this platform is supported
       rpm_dir="./${Arch}_${OS_Type}_${GCC_Ver}.XX"
       if [ ! -d "$rpm_dir" ];then
           log_message "ERROR: $rpm_dir not found"
           log_message "This apps kit does not support this platform"
           exit
       fi

       # Install dfc helper module
       log_message "Installing lpfcdfc helper module..."
      	./lpfcdfc-install -i
       EXITCODE=$?
       if [ ${EXITCODE} -ne 0 ];then
             if [ ${EXITCODE} -eq 10 ];then
                 log_message ""
                 log_message "Success with WARNINGS installing Application Helper"
                 log_message "Module.  Please refer to the following file for more"
                 log_message "information about these warnings:"
                 log_message "   ${LOGFILE}"
                 log_message ""
      		 else
                log_message ""
                log_message "ERROR: Application Helper Module installation failed."
                log_message ""
                exit
      		 fi
       else
	       log_message "Success installing Application Helper Module"
       fi

       # Uninstall any previous HBAnyware install
       log_message "Scanning for previous HBAnyware installations ..."
       Cur_HBA=`rpm -qa | grep HBAnyware | grep -v SSC`
       if [ -n "$Cur_HBA" ];then
           log_message "Uninstalling HBAnyware -- $Cur_HBA -- ..."
           rpm -e --nodeps "$Cur_HBA"
           log_message ""
       fi
 
       # Uninstall any previous AutoPilot install
       Cur_AP=`rpm -qa | grep AutoPilot`
       if [ -n "$Cur_AP" ];then
           log_message "Uninstalling AutoPilot -- $Cur_AP -- ..."
           rpm -e --nodeps "$Cur_AP"
           log_message ""
       fi
 
       # Install dfc libs and utility
       dfc_test=`find . -name lpfcutil*.tgz`
       if [ -n "$dfc_test" ];then
          log_message "Untarring lpfcutil tgz file..."
          tar xfz lpfcutil*.tgz
       else
          log_message "Untarring lpfcutil .tar.gz file..."
          tar xfz lpfcutil*.tar.gz
       fi

       if [ ! -d /usr/sbin/lpfc ];then
           mkdir /usr/sbin/lpfc
       fi
       if [ "$Arch" = "ppc64" ]; then
           cp -f ./lpfcutil/ppc/${OS_Ver}/lib* /usr/lib
           cp -f ./lpfcutil/ppc64/${OS_Ver}/lib* /usr/lib64
           cp -f ./lpfcutil/ppc/${OS_Ver}/dfc /usr/sbin/lpfc
       else
           # If amd64 then install 64bit libs also
           if [ "$Arch" = "x86_64" ]; then
               cp -f ./lpfcutil/i386/${OS_Ver}/lib* /usr/lib
               cp -f ./lpfcutil/${Arch}/${OS_Ver}/lib* /usr/lib64
               cp -f ./lpfcutil/${Arch}/${OS_Ver}/dfc /usr/sbin/lpfc
           else
               cp -f ./lpfcutil/${Arch}/${OS_Ver}/lib* /usr/lib
               cp -f ./lpfcutil/${Arch}/${OS_Ver}/dfc /usr/sbin/lpfc
           fi
       fi
       if [ -f "./scripts/lun_scan" ]; then
	       cp -f ./scripts/lun_scan /usr/sbin/lpfc
	   else
           if [ -f "./lun_scan" ]; then
	           cp -f ./lun_scan /usr/sbin/lpfc
	       fi
       fi

       # Install HBAAPI libs
       log_message "Untarring hbaapi libs tgz file..."
       tar xfz hbaapi*.tgz
       if [ "$Arch" = "i386" ]; then
           cp -f ./hbaapi/${Arch}/32bit/lib* /usr/lib
       fi
       if [ "$Arch" = "ia64" ]; then
           cp -f ./hbaapi/${Arch}/64bit/lib* /usr/lib
       fi
       if [ "$Arch" = "ppc64" ]; then
           cp -f ./hbaapi/${Arch}/32bit/lib* /usr/lib
           if [ "$Kernel_Ver" = "26" ]; then
               cp -f ./hbaapi/${Arch}/64bit/lib* /usr/lib64
           else
               cp -f ./hbaapi/${Arch}/64bit/${OS_Ver}/lib* /usr/lib64
	       fi
       fi
       if [ "$Arch" = "x86_64" ]; then
           cp -f ./hbaapi/i386/32bit/lib* /usr/lib
           cp -f ./hbaapi/amd64/64bit/lib* /usr/lib64
       fi


       # Install lputil utility
       log_message "Untarring lputil tgz file..."
       tar xfz lputil*.tgz
       if [ "$Arch" = "x86_64" ]; then
            cp -f ./lputil/i386/lputil* /usr/sbin/lpfc
	   else
            cp -f ./lputil/${Arch}/lputil* /usr/sbin/lpfc
	   fi
       chmod +x /usr/sbin/lpfc/lputil*


       # Install HBAnyware
       cd "$rpm_dir"
       rpm -U HBAnyware-*.rpm
       cd ..
#       log_message ""

       # make directory for semaphore file locks
       if [ ! -d /usr/sbin/hbanyware/misc ];then
           mkdir /usr/sbin/hbanyware/misc
       fi

       # Install JRE
       cur_dir=`pwd`
       jre_dir="./jre_${Arch}"
       if [ ! -d "$jre_dir" ];then
           log_message "ERROR: JRE dir $jre_dir not found. Local JRE can not be installed"
           exit
       fi
       if [ ! -d /usr/sbin/hbanyware/jre ];then
           mkdir /usr/sbin/hbanyware/jre
       fi
       cd /usr/sbin/hbanyware/jre
       log_message "Untarring HBAnyware local jre..."
       tar xfz ${cur_dir}/${jre_dir}/jre_hbanyware.tgz
       cd "$cur_dir"
#       log_message ""

      	# obtain user selections for remote management
       cd /usr/sbin/hbanyware
     	./set_operating_mode 45
       cd "$cur_dir"

       # remove script that changes operating mode if installer selects this option
       no_mode_change=`cat /etc/emulexDiscConfig | grep ClientOperationMode | grep locked`
	   if [ -n "$no_mode_change" ];then
	      rm -f /usr/sbin/hbanyware/set_operating_mode
	   fi
       log_message ""
       log_message "HBAnyware installation complete."
       log_message ""
       log_message "Type:   /usr/sbin/hbanyware/hbanyware"
       log_message "to run HBAnyware GUI client."
       log_message ""
	fi
fi


# Clean up
#log_message "Cleaning up..."
cd "$cur_dir"
rm -rf ./*.XX
rm -rf ./jre_*
rm -rf ./etc
rm -rf lputil
rm -rf hbaapi
rm -rf lpfcutil
#log_message ""

cd ..

